﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Linq;
namespace DataModel.Commands
{
    public class ConditionCommand:ICommand
    {
        Action<object> action;
        ICondition[] conditions;
        public ConditionCommand(Action Action, params ICondition[] Conditions)
        {
            action = (o)=>Action();
            conditions = Conditions;
            foreach (var condition in conditions)
            {
                condition.AddHandler(this);
            }
        }
        public ConditionCommand(Action<object> Action, params ICondition[] Conditions)
        {
            action = Action;
            conditions = Conditions;
            foreach (var condition in conditions)
            {
                condition.AddHandler(this);
            }
        }
        internal void onCanExecuteChanged()
        {
            var handler = CanExecuteChanged;
            if (handler != null)
            {
                handler(this, EventArgs.Empty);
            }
        }
        #region ICommand Members

        public bool CanExecute(object parameter)
        {
            return conditions.All((C) => C.value(parameter));
        }

        public event EventHandler CanExecuteChanged;

        public void Execute(object parameter)
        {
            action(parameter);
        }

        #endregion
    }
}
